package com.ysstech.system.controller;


import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.system.enums.SystemEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * <p>
 * 字典值前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2021-01-11
 */
@Controller
@Slf4j
@Api(tags = "字典值管理api")
@RequestMapping("/dicvalue")
public class DicvalueController extends BaseController {


    @YssPermissions(value = {"system:dicvalue"})
    @ApiOperation(value = "跳转字典管理页面")
    @YssLog(module = "字典管理", remark = "跳转字典管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "system/dicvalue";
    }


    @YssPermissions(value = {"system:dicvalue"})
    @ApiOperation(value = "获取字典信息")
    @YssLog(module = "字典管理", remark = "获取字典信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(Dicvalue dicvalue, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("查询字典详细值信息成功",
                dicValueService.queryAllDicValue(dicvalue, this.getPage(page, rows)));
    }

    @YssPermissions(value = {"system:dicvalue"})
    @ApiOperation(value = "获取字典页面机构的状态的值")
    @YssLog(module = "字典管理", remark = "获取字典页面机构的状态的值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        List<Dicvalue> sysStatus = dicValueService.getCacheDicValueList(SystemEnum.SYS_STATUS.getCode());
        Map<String, Object> map = new HashMap<>();
        map.put("sysStatus", sysStatus);
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }
    /**
     * 添加字典对应值
     *
     * @param dicvalue
     * @return
     */
    @YssPermissions(value = {"system:dicvalue:add"})
    @ApiOperation(value = "添加字典对应值")
    @YssLog(module = "字典管理", remark = "添加字典对应值")
    @PostMapping(value = "/addValue")
    @ResponseBody
    public RestResult addValue(Dicvalue dicvalue) throws Exception {
        redisCacheManager.removeCache(SystemEnum.YSS_DICINFO.getCode());
        dicvalue.setId(DateUtil.generateUUID());
        dicvalue.setAddTime(LocalDateTime.now());
        dicValueService.save(dicvalue);
        return ResultGenerator.getSuccessResult("新增字典信息成功！");
    }

    /**
     * 修改字典对应值
     *
     * @param dicvalue
     * @return
     */
    @YssPermissions(value = {"system:dicvalue:edit"})
    @ApiOperation(value = "修改字典对应值")
    @YssLog(module = "字典管理", remark = "修改字典对应值")
    @PostMapping(value = "/editValue")
    @ResponseBody
    public RestResult editValue(Dicvalue dicvalue) throws Exception {
        redisCacheManager.removeCache(SystemEnum.YSS_DICINFO.getCode());
        dicValueService.updateById(dicvalue);
        return ResultGenerator.getSuccessResult("修改字典信息成功！");
    }

    /**
     * 删除字典对应值
     *
     * @param ids
     * @return
     */
    @YssPermissions(value = {"system:dicvalue:del"})
    @ApiOperation(value = "删除字典对应值")
    @YssLog(module = "字典管理", remark = "删除字典对应值")
    @PostMapping(value = "/delValue")
    @ResponseBody
    public RestResult delValue(String ids) throws Exception {
        redisCacheManager.removeCache(SystemEnum.YSS_DICINFO.getCode());
        if (StringUtils.isBlank(ids)) {
            return ResultGenerator.getBusinessResult("删除字典信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(ids, String.class);
        dicValueService.removeByIds(list);
        return ResultGenerator.getSuccessResult("删除字典信息成功！");
    }


    /**
     * 字典详细信息判断字典的值是否已经存在
     *
     * @param dicvalue
     * @return
     */
    @ApiOperation(value = "字典的值是否已经存在")
    @YssLog(module = "字典管理", remark = "字典的值是否已经存在")
    @PostMapping(value = "/checkDicValue")
    @ResponseBody
    public RestResult checkSysDicValue(Dicvalue dicvalue) throws Exception {
        if (null == dicvalue || StringUtils.isBlank(dicvalue.getCode()) || StringUtils.isBlank(dicvalue.getValue())) {
            return ResultGenerator.getBusinessResult("获取字典详细信息值失败，请稍后重试！");
        }
        QueryWrapper<Dicvalue> wrapper = new QueryWrapper<>();
        wrapper.eq("CODE", dicvalue.getCode());
        wrapper.eq("VALUE", dicvalue.getValue());
        int count = dicValueService.count(wrapper);
        return ResultGenerator.getSuccessResult("获取字典值成功", count > 0 ? true : false);
    }

}
